﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Common.Component;

namespace Runda.EShop.Web.Order
{
    public partial class OrderConfirm : BasePage
    {
        public Guid OrderID
        {
            get
            {
                if (Request.QueryString["OrderID"] != null)
                {
                    return new Guid(Request.QueryString["OrderID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                Initialize();
            }
        }

        private void Initialize()
        {
          
            GetOrderPatch();
        }

        private void GetOrderPatch()
        {
            if (OrderID == Guid.Empty) return;
            BizOrderDetailPatchBll bll = new BizOrderDetailPatchBll();
            DataTable dt = bll.GetOrderDetailPatch(OrderID);
            gvOrderPatch.DataSource = dt;
            gvOrderPatch.DataBind();
        }

        protected void gvOrderPatch_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button btnConfirm = e.Row.FindControl("btnConfirm") as Button;
                DataRowView dv = e.Row.DataItem as DataRowView;
                if (dv["ConfirmStatus"].ToString() == "已确认")
                {
                    btnConfirm.Visible = false;
                }
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            Button btnConfirm = sender as Button;
            int index = ((GridViewRow)((btnConfirm).Parent.Parent)).RowIndex;
            DecimalTextBox txtActualQty = (DecimalTextBox)gvOrderPatch.Rows[index].FindControl("txtActualQty");
            if (!txtActualQty.Value.HasValue)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('请输入确认数量!');", true);
                return;
            }

            HiddenField txtAssignQty = gvOrderPatch.Rows[index].FindControl("txtAssignQty") as HiddenField;

            if (txtActualQty.Value.Value > Convert.ToDecimal(txtAssignQty.Value))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('确认数量不能大于订购数量!');", true);
                return;
            }

            Guid ID = new Guid(btnConfirm.CommandArgument.ToString());

            BizOrderDetailPatchBll bll = new BizOrderDetailPatchBll();

            bll.UpdateOrderDetailPatchStatus(ID, "已确认", txtActualQty.Value.Value);

            GetOrderPatch();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            OrderBll bll = new OrderBll();
            //检查所有订单是否确认
            int Count = bll.Count<BizOrderDetailPatchEntity>(" AND OrderID='" + OrderID + "' AND ConfirmStatus='未确认'");
            if (Count > 0)
            { 
                ClientScript.RegisterStartupScript(this.GetType(), "erroe", "alert('请先确认订单.');", true);
                return;
            }
            //确认总订单
            bll.ConfirmOrder(OrderID,base.User.Identity.LoginUser);

            //ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('订单确认成功.');window.location='OrderList.aspx'", true);
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "success", "alert('订单确认成功.');window.location='OrderList.aspx';", true);
        }
        
    }
}